﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Asset_Manager
{
    public partial class frmMDIMain : Form
    {
        public static ServiceReference1.User _user = new ServiceReference1.User();
        public frmMDIMain()
        {
            InitializeComponent();
            frmLogin l = new frmLogin(_user);
            l.StartPosition = FormStartPosition.CenterScreen;
            l.ShowDialog();
            FillTree();
        }

        private void FillTree()
        {
            ServiceReference1.IService1 sv = new ServiceReference1.Service1Client();
            DataTable dParent = sv.GetParentNodes(1);
            foreach (DataRow rP in dParent.Rows)
            {
                TreeNode p = new TreeNode();
                p.Text = rP["Metadata"].ToString();
                p.ImageIndex = 0;
                DataTable dChild = sv.GetChildNodes(rP["Metadata"].ToString());
                foreach (DataRow rC in dChild.Rows)
                {
                    TreeNode c = new TreeNode();
                    c.Text = rC["Name"].ToString();
                    c.Tag = rC["pkAssetID"].ToString();
                    c.ImageIndex = 0;
                    p.Nodes.Add(c);
                }
                tvMain.Nodes.Add(p);
            }
        }

        //buncha junk below
        //private void ShowNewForm(object sender, EventArgs e)
        //{
        //    Form childForm = new Form();
        //    childForm.MdiParent = this;
        //    childForm.Text = "Window " + childFormNumber++;
        //    childForm.Show();
        //}

        //private void OpenFile(object sender, EventArgs e)
        //{
        //    OpenFileDialog openFileDialog = new OpenFileDialog();
        //    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        //    openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        //    if (openFileDialog.ShowDialog(this) == DialogResult.OK)
        //    {
        //        string FileName = openFileDialog.FileName;
        //    }
        //}

        //private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    SaveFileDialog saveFileDialog = new SaveFileDialog();
        //    saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        //    saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        //    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
        //    {
        //        string FileName = saveFileDialog.FileName;
        //    }
        //}

        //Below should use a tab container

        private void frmMDIMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild == null)
                tabForms.Visible = false;
            // If no any child form, hide tabControl 
            else
            {
                this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
                // Child form always maximized 
                // If child form is new and no has tabPage, 
                // create new tabPage 
                if (this.ActiveMdiChild.Tag == null)
                {
                    // Add a tabPage to tabControl with child 
                    // form caption 
                    TabPage tp = new TabPage(this.ActiveMdiChild.Text);
                    tp.Tag = this.ActiveMdiChild;
                    tp.Parent = tabForms;
                    tabForms.SelectedTab = tp;

                    this.ActiveMdiChild.Tag = tp;
                    this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
                }
                if (!tabForms.Visible) tabForms.Visible = true;
            }
        }

        private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
        {
            ((sender as Form).Tag as TabPage).Dispose();
        }

        private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((tabForms.SelectedTab != null) && (tabForms.SelectedTab.Tag != null))
            {
                (tabForms.SelectedTab.Tag as Form).Select();
            }
        }

        private void loginFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin frm = new frmLogin(_user);
            //frm.MdiParent = this;
            frm.ShowDialog();
            updateStatusStrip();

        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void frmMDIMain_Load(object sender, EventArgs e)
        {
            //frmLogin frm = new frmLogin(_user);
            //// frm.MdiParent = this;
            //frm.ShowDialog();
            //updateStatusStrip();
            //loadTreeView();
            ////TODO fill tree view with assets after login process is over
            ////tvMain.Nodes;
        }

        private void loadTreeView()
        {
     
            
        }

        private void reviewAssetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAssetViewer frm = new frmAssetViewer();
            frm.MdiParent = this;
            frm.Show();
        }

        private void newAssetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNewAsset frm = new frmNewAsset();
            frm.MdiParent = this;
            frm.Show();
        }

        private void tvMain_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                MessageBox.Show(e.Node.Tag.ToString());
                frmAssetViewer i = new frmAssetViewer();
                i.MdiParent = this;
                i.Show();
            }

        }

        private void updateStatusStrip()
        {
            if (_user._userName != null)
            {
                tslblMain.Text = "Welcome " + _user._userName.ToString() + "!";
            }
            else
            {
                tslblMain.Text = "You are not logged in, please log in!";
            }        
        }


        //    private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        //    {
        //        this.Close();
        //    }

        //    private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        //    {
        //    }

        //    private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        //    {
        //    }

        //    private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        //    {
        //    }
        //}

        //private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    LayoutMdi(MdiLayout.ArrangeIcons);
        //}
    }
}
